package top.anka1.ims.BouncingShapes.bounceboxapp;

import top.anka1.ims.BouncingShapes.bouncebox.*;
import top.anka1.ims.ZooManagementSystem.Zoo;
import top.anka1.ims.ZooManagementSystem.Animal;

import java.awt.Color;

public class ZooSplitBarVisualizer {
    private BounceBox box;
    private int width;
    private int height;
    private static final int MAX_ANIMALS_PER_ZOO = 10;


    public ZooSplitBarVisualizer(int width, int height) {
        this.width = width;
        this.height = height;
        this.box = new BounceBox(width, height);
    }

    /**
     * Visualizes animals from both zoos in a split view
     * @param southernZoo The southern zoo
     * @param northernZoo The northern zoo
     */
    public void visualizeZooAnimals(Zoo southernZoo, Zoo northernZoo) {

        // Calculate positions and dimensions
        int centerX = width / 2;
        int barWidth = 30;
        int barSpacing = 10;
        int maxBarHeight = height - 100; // Leave space for labels

        // Draw dividing line
        Rectangle divider = new Rectangle(centerX - 1, 20, 2, height - 40);
        divider.setColor(Color.BLACK);
        box.addShape(divider);

        // Add title labels
        addTextLabel("Southern Zoo (Red)", centerX / 2, 20, Color.RED);
        addTextLabel("Northern Zoo (Blue)", centerX + centerX / 2, 20, Color.BLUE);

        // Visualize southern zoo animals (left side - red)
        int southernCount = southernZoo.getCounter();
        int startX = centerX - barWidth - barSpacing;

        for (int i = 0; i < southernCount; i++) {
            Animal animal = southernZoo.getAnimal(i);
            int barX = startX - i * (barWidth + barSpacing);
            int barHeight = maxBarHeight; // Fixed height for all bars

            Rectangle bar = new Rectangle(
                    barX - barWidth/2,
                    height - barHeight - 30,
                    barWidth,
                    barHeight
            );
            bar.setColor(Color.RED);
            box.addShape(bar);

            // Add animal name label
            addTextLabel(animal.getName(), barX, height - 15, Color.BLACK);
        }

        // Visualize northern zoo animals (right side - blue)
        int northernCount = northernZoo.getCounter();
        startX = centerX + barWidth + barSpacing;

        for (int i = 0; i < northernCount; i++) {
            Animal animal = northernZoo.getAnimal(i);
            int barX = startX + i * (barWidth + barSpacing);
            int barHeight = maxBarHeight; // Fixed height for all bars

            Rectangle bar = new Rectangle(
                    barX - barWidth/2,
                    height - barHeight - 30,
                    barWidth,
                    barHeight
            );
            bar.setColor(Color.BLUE);
            box.addShape(bar);

            // Add animal name label
            addTextLabel(animal.getName(), barX, height - 15, Color.BLACK);
        }

        box.start();
    }

    /**
     * Helper method to add text labels (using small rectangles as simple text representation)
     */
    private void addTextLabel(String text, int x, int y, Color color) {
        // For each character in the text, add a small square
        int charWidth = 5;
        int startX = x - (text.length() * charWidth) / 2;

        for (int i = 0; i < text.length(); i++) {
            Square charBlock = new Square(startX + i * charWidth, y, 3);
            charBlock.setColor(color);
            box.addShape(charBlock);
        }
    }

    /**
     * Example usage method
     */
    public static void exampleUsage() {
        // Create zoos and add animals (like in ZooManagementSystem)
        Zoo southernZone = new Zoo("Southern-Zone Zoo");
        southernZone.addAnimal(new Animal("Simba", "African Lion", 6));
        southernZone.addAnimal(new Animal("Dumbo", "African Elephant", 12));
        southernZone.addAnimal(new Animal("Luna", "Gray Wolf", 4));
        southernZone.addAnimal(new Animal("Poe", "Raven", 3));
        southernZone.addAnimal(new Animal("Benny", "Grizzly Bear", 8));


        Zoo northernZone = new Zoo("Northern-Zone Zoo");
        northernZone.addAnimal(new Animal("Arctic", "Polar Bear", 7));
        northernZone.addAnimal(new Animal("Blizzard", "Snow Leopard", 5));
        northernZone.addAnimal(new Animal("Frost", "Arctic Fox", 3));
        northernZone.addAnimal(new Animal("Penguin", "Emperor Penguin", 4));
        northernZone.addAnimal(new Animal("Aurora", "Caribou", 6));

        // Create visualizer and display the split view
        ZooSplitBarVisualizer visualizer = new ZooSplitBarVisualizer(800, 600);
        visualizer.visualizeZooAnimals(southernZone, northernZone);
    }

    public static void main(String[] args) {
        exampleUsage();
    }
}
